<?php
/**
 * Created by PhpStorm.
 * User: renyi
 * Date: 2017/10/13
 * Time: 上午3:12
 */

namespace App\Core;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller as RoutingController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Support\Facades\Log;

class BaseController extends RoutingController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    use CommonPoolMethod;

    const COMMON_NOT_SET = "未设定";
    const PAGE_HEADER = "";
    const PAGE_DESC_INDEX = "列表";
    const PAGE_DESC_CREATE = "新建";
    const PAGE_DESC_UPDATE = "编辑";

    /**
     * 存放被预先加载的id
     * @var array
     */
    protected $idBundle = [];

    /**
     * 需要被预加载的id名称列表
     * @var array
     */
    protected $idNames = [];

    /**
     * http请求对象
     * @var null
     */
    protected $httpRequest = null;

    /**
     * 预加载
     *
     * @param Request $request
     */
    protected function preload(Request $request)
    {
        $this->httpRequest = $request;

        // Log::info("idNames : ", $this->idNames);
        foreach ($this->idNames as $key => $isPrimaryId)
        {
            if ($isPrimaryId)
            {
                $this->idBundle['primaryId'] = $request->route($key);
            }
            else
            {
                $this->idBundle[camel_case($key)] = $request->route($key);
            }
        }
        // Log::info("idBundle : ", $this->idBundle);
    }

    /**
     * 支持用魔法方法访问 idBundle
     *
     * @param $name
     * @return mixed|string
     */
    public function __get($name)
    {
        if (array_key_exists($name, $this->idBundle))
        {
            return $this->idBundle[$name];
        }
        else
        {
            return "";
        }
    }
}